package com.powernode.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.domain.UserAddr;
import com.powernode.mapper.UserAddrMapper;
import com.powernode.service.UserAddrService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 内容:
 * 作者: cy
 */
@Service
@CacheConfig(cacheNames = "com.powernode.service.impl.UserAddrServiceImpl")
@Slf4j
public class UserAddrServiceImpl extends ServiceImpl<UserAddrMapper, UserAddr> implements UserAddrService {

    @Autowired
    private UserAddrMapper userAddrMapper;


    /**
     * 查询当前会员的收货地址列表
     *
     * @param openId
     * @return
     */
    @Override
    @Cacheable(key = "#openId")
    public List<UserAddr> selectUserAddrList(String openId) {
        return userAddrMapper.selectList(new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getUserId, openId)
                .eq(UserAddr::getStatus, 1)
                .orderByDesc(UserAddr::getCommonAddr, UserAddr::getCreateTime)
        );
    }

    /**
     * 新增会员的收货地址
     *
     * @param userAddr
     */
    @Override
    @CacheEvict(key = "#userAddr.userId")
    public void saveUserAddr(UserAddr userAddr) {
        log.info("会员新增地址{}", JSON.toJSONString(userAddr));
        userAddr.setCreateTime(new Date());
        userAddr.setStatus(1);
        userAddr.setUpdateTime(new Date());
        userAddr.setVersion(0);
        Integer count = userAddrMapper.selectCount(new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getUserId, userAddr.getUserId())
                .eq(UserAddr::getCommonAddr, 1)
        );
        if (count <= 0) {
            // 默认地址 如果之前没有地址 那么这个地址就是默认的
            userAddr.setCommonAddr(1);
        }
        userAddrMapper.insert(userAddr);
    }

    /**
     * 修改会员的默认收货地址
     *
     * @param openId
     * @param id
     */
    @Override
    @CacheEvict(key = "#openId")
    @Transactional(rollbackFor = RuntimeException.class)
    public void changeDefaultAddr(String openId, Long id) {
        // 先查询用户的默认收货地址
        UserAddr newAddr = userAddrMapper.selectById(id);
        if (newAddr.getCommonAddr().equals(1)) {
            return;
        }
        // 还要把原来的默认地址 换掉
        UserAddr oldAddr = new UserAddr();
        oldAddr.setCommonAddr(0);
        oldAddr.setUpdateTime(new Date());
        userAddrMapper.update(oldAddr, new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getUserId, openId)
                .eq(UserAddr::getCommonAddr, 1)
        );
        newAddr.setCommonAddr(1);
        newAddr.setUpdateTime(new Date());
        userAddrMapper.updateById(newAddr);
    }
}
